package Gui;

import Entity.Admin;
import Model.RoleTableModel;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author CONG
 */
public class DialogAddAdmin extends javax.swing.JDialog {

    private SystemManager sm;
    private RoleTableModel modelRole;

    /** Creates new form DialogAddAdmin */
    public DialogAddAdmin(SystemManager parent, boolean modal, RoleTableModel model) {
        super(parent, modal);
        this.sm = parent;
        this.modelRole = model;
        initComponents();
        for (int i = 0; i < modelRole.getSize(); i++) {
            cbbPermission.addItem(modelRole.getRole(i).getName());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtFullName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        cbbPermission = new javax.swing.JComboBox();
        btnSave = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        txtPass = new javax.swing.JPasswordField();
        txtConfirmPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add New Manager");

        jLabel1.setFont(new java.awt.Font("Arial", 1, 24));
        jLabel1.setForeground(new java.awt.Color(0, 153, 255));
        jLabel1.setText("Add New Manager");

        jLabel2.setFont(new java.awt.Font("Arial", 0, 14));
        jLabel2.setForeground(new java.awt.Color(255, 51, 51));
        jLabel2.setText("(*) Need Information");

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setForeground(new java.awt.Color(44, 44, 44));
        jLabel3.setText("User Name:");

        txtUserName.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel4.setForeground(new java.awt.Color(44, 44, 44));
        jLabel4.setText("Password:");

        jLabel5.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel5.setForeground(new java.awt.Color(44, 44, 44));
        jLabel5.setText("Confirm Password:");

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel6.setForeground(new java.awt.Color(44, 44, 44));
        jLabel6.setText("Full Name:");

        txtFullName.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel7.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel7.setForeground(new java.awt.Color(44, 44, 44));
        jLabel7.setText("Address:");

        txtAddress.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel8.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel8.setForeground(new java.awt.Color(44, 44, 44));
        jLabel8.setText("Email:");

        txtEmail.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setForeground(new java.awt.Color(44, 44, 44));
        jLabel9.setText("Phone:");

        txtPhone.setPreferredSize(new java.awt.Dimension(59, 22));
        txtPhone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPhoneKeyTyped(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setForeground(new java.awt.Color(44, 44, 44));
        jLabel10.setText("Permission");

        btnSave.setFont(new java.awt.Font("Arial", 1, 12));
        btnSave.setForeground(new java.awt.Color(44, 44, 44));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add-16x16.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnReset.setFont(new java.awt.Font("Arial", 1, 12));
        btnReset.setForeground(new java.awt.Color(44, 44, 44));
        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Button-Refresh-16x16.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Arial", 1, 12));
        btnCancel.setForeground(new java.awt.Color(44, 44, 44));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Cancel-16x16.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        txtPass.setPreferredSize(new java.awt.Dimension(111, 22));

        txtConfirmPassword.setPreferredSize(new java.awt.Dimension(6, 22));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtFullName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtAddress, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                            .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(cbbPermission, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtPhone, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(btnSave)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnReset)))
                                .addGap(18, 18, 18)
                                .addComponent(btnCancel))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtConfirmPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtPass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addGap(41, 41, 41))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtConfirmPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(cbbPermission, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
// TODO add your handling code here:
    txtAddress.setText("");
    txtConfirmPassword.setText("");
    txtPass.setText("");
    txtEmail.setText("");
    txtFullName.setText("");
    txtPhone.setText("");
    txtUserName.setText("");
    cbbPermission.setSelectedIndex(0);
}//GEN-LAST:event_btnResetActionPerformed

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed
    private String address;
    private String name;
    private String fullName;
    private String phone;
    private String pass;
    private String repass;
    private String email;
    private String permission;
    private String str1;
    private String str2;
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
// TODO add your handling code here:
    String email = txtEmail.getText();
    if (sm.checkEmailAdmin(email)) {
        JOptionPane.showConfirmDialog(this, "Email existed. Please choice another email address", "Error", JOptionPane.ERROR_MESSAGE);
        txtEmail.requestFocus();
        return;
    } else if (checkValid()) {
        pass = str1;
        repass = str2;
        int roleCode = 0;
        for (int i = 0; i < modelRole.getSize(); i++) {
            if (modelRole.getRole(i).getName() == null ? permission == null : modelRole.getRole(i).getName().equals(permission)) {
                roleCode = modelRole.getRole(i).getRoleCode();
                break;
            }
        }
        Admin ad = new Admin(name, pass, fullName, address, phone, email, roleCode);
        sm.addAdmin(ad);
        JOptionPane.showMessageDialog(this, "Manage added successfully.");
        this.dispose();
    }
}//GEN-LAST:event_btnSaveActionPerformed

private void txtPhoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPhoneKeyTyped
// TODO add your handling code here:
    String number = "0123456789";
    if (number.indexOf(evt.getKeyChar()) == -1) {
        evt.consume();
    }
}//GEN-LAST:event_txtPhoneKeyTyped
    public boolean checkValid() {
        address = txtAddress.getText();
        name = txtUserName.getText();
        fullName = txtFullName.getText();
        phone = txtPhone.getText();
        email = txtEmail.getText();
        char[] ch = txtPass.getPassword();
        str1 = String.valueOf(ch);
        char[] ch2 = txtConfirmPassword.getPassword();
        str2 = String.valueOf(ch2);
        permission = (String) cbbPermission.getSelectedItem();
        if (name.isEmpty()) {
            JOptionPane.showConfirmDialog(this, "Name can not blank.", "Error", JOptionPane.ERROR_MESSAGE);
            txtUserName.requestFocus();
            return false;
        } else if (str1.isEmpty()) {
            JOptionPane.showConfirmDialog(this, "Password can not blank.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPass.requestFocus();
            return false;
        } else if (str1.length()<=4) {
            JOptionPane.showConfirmDialog(this, "Password must be greater 4.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPass.requestFocus();
            return false;
        }else if (str2.isEmpty()) {
            JOptionPane.showConfirmDialog(this, "Confirm password can not blank.", "Error", JOptionPane.ERROR_MESSAGE);
            txtConfirmPassword.requestFocus();
            return false;
        } else if (fullName.isEmpty()) {
            JOptionPane.showConfirmDialog(this, "FullName can not blank.", "Error", JOptionPane.ERROR_MESSAGE);
            txtFullName.requestFocus();
            return false;
        } else if (address.isEmpty()) {
            JOptionPane.showConfirmDialog(this, "Address can not blank.", "Error", JOptionPane.ERROR_MESSAGE);
            txtAddress.requestFocus();
            return false;
        } else if (!Pattern.matches("0[\\d]{8,11}", phone)) {
            JOptionPane.showConfirmDialog(this, "Phone must be 8 - 11 digits and start by 0.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPhone.requestFocus();
            return false;
        } else if (!Pattern.matches(".+@.+\\.[a-z]+", email)) {
            JOptionPane.showMessageDialog(this, "Email must be abc@xyz.com", "Error", JOptionPane.ERROR_MESSAGE);
            txtEmail.requestFocus();
            return false;
        } else if (!str1.equalsIgnoreCase(str2)) {
            JOptionPane.showMessageDialog(this, "Pass and repass no same.", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbPermission;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JPasswordField txtConfirmPassword;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
